var mainApp = angular.module("mainApp");

mainApp.controller("roleController", function($rootScope, $scope, $http) {
	var rm = $scope.rm = {
		roleList : {}
	};

	rm.query = function() {
		$http.post("system/role/list.do").success(function(data) {
			rm.roleList = data.roleList;
		})
	};

	rm.query();

	rm.getids = function() {
		var id = [];
		angular.forEach(rm.roleList, function(role) {
			if (role.isChecked) {
				id.push(role.id);
			}
		});
		return id;
	}

	rm.del = function() {
		var ids = rm.getids();
		if (ids.length == 0) {
			$rootScope.alert("请至少选择一条记录！");
			return;
		}
		$rootScope.confirm("该操作无法回滚，确认删除？", function() {
			$http.post("system/role/del.do", {
				ids : ids.join()
			}).success(function(data) {
				$("#delete").modal("hide");
				rm.query();
			})
		})
	}

	rm.add = function() {
		rm.title = '角色添加';
		rm.isEdit = false;
		rm.role = [];
		rm.resourceSelected = [];
	}

	rm.edit = function(role) {
		rm.title = '角色修改';
		rm.isEdit = true;
		rm.role = angular.copy(role);
		rm.resourceSelected = rm.role.resourceList;
	}

	rm.addOrEdit = function(form) {
		rm.showError = true;
		form.$setDirty();
		if (form.$valid) {
			rm.role.resids = rm.getResIds(rm.reslist);
			rm.role.resourceList = [];
			$http.post("system/role/addOrEdit.do", rm.role).success(
					function(data) {
						rm.showMessage = true;
						rm.result = data.result;
						if (data.result == 1) {
							rm.message = '操作成功!';
							form.$setPristine();
						} else if (data.result == 2) {
							rm.message = '角色名已经存在 !';
						} else {
							rm.message = '操作失败！';
						}
					})
			rm.showError = false;
		}
	}

	$("#roleDetail").on("show.bs.modal", function() {
		$http.post("system/role/reslist.do").success(function(data) {
			rm.reslist = data.reslist;
			rm.setSelected(rm.reslist);
		})
	});

	rm.setSelected = function(all) {
		angular.forEach(all, function(it) {
			angular.forEach(rm.resourceSelected, function(item) {
				if (item.id == it.id) {
					it.isSelected = true;
				}
			})
			rm.setSelected(it.resourceList);
		})
	}

	$("#roleDetail").on("hidden.bs.modal", function() {
		rm.showMessage = false;
		rm.query();
	});

	rm.getResIds = function(all) {
		var ids = "";
		angular.forEach(all, function(it) {
			var i = rm.getResIds(it.resourceList);
			if (i != "") {
				ids += i + ",";
			}
			if (it.isSelected) {
				ids += it.id + ",";
			}
		})
		return ids;
	}

	rm.checkChange = function(item) {
		rm.checkSon(item);
		rm.checkPar(rm.reslist);
	}

	rm.checkPar = function(all) {
		var b = false;
		if (angular.isDefined(all) && all != null) {
			for (var i = 0; i < all.length; i++) {
				var bool = rm.checkPar(all[i].resourceList);
				if (bool) {
					all[i].isSelected = bool;
				}
				if (all[i].isSelected) {
					b = true;
				}
			}
		}
		return b;
	}

	rm.checkSon = function(item) {
		if (angular.isDefined(item.resourceList)) {
			angular.forEach(item.resourceList, function(it) {
				it.isSelected = item.isSelected;
				rm.checkSon(it);
			})
		}
	}

})